package org.noote.test_bluetooth;

import org.noote.libs.bluetooth.DeviceList_Activity;
import org.noote.libs.bluetooth.SPPClient;
import org.noote.libs.bluetooth.SPPEvent_Client;
import org.noote.libs.roby.comm.BluetoothCommandProtocol;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class TestBluetooth_Activity extends Activity {
	private static final int REQUEST_ENABLE_BT = 1;

	protected static final int GETTING_DEVICE = 0;

	protected static final int MESSAGE_IND = 0;
	protected static final int MESSAGE_FINISH = 1;
	protected static final int MESSAGE_READ = 2;
	
	protected static final int MESSAGE_UI_CONNECTED=0;
	protected static final int MESSAGE_UI_DISCONNECTED=1;
	protected static final int MESSAGE_UI_GETERROR=2;
	protected static final int MESSAGE_UI_GETTEXT=3;
	protected static final int MESSAGE_UI_GETCMD=4;
	
	BluetoothAdapter mBluetoothAdapter = null;
	SPPClient _SPPClient = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        _SPPClient = new SPPClient(mBTHandler);
        
        Button buttonScan = (Button)findViewById(R.id.buttonScan);
        buttonScan.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
	           Intent intent = new Intent(getApplicationContext(), DeviceList_Activity.class);
	           startActivityForResult(intent, GETTING_DEVICE);
			}
        });
        Button buttonTest = (Button)findViewById(R.id.buttonTest);
        buttonTest.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
	           LaunchTest();
			}
        });
        Button buttonCancel = (Button)findViewById(R.id.buttonCancel);
        buttonCancel.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
				CancelTest();
			}
        });
        Button buttonSend = (Button)findViewById(R.id.buttonSend);
        buttonSend.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
				EditText editSend = (EditText)findViewById(R.id.editSend);
				String sBlabla = editSend.getText().toString();
				editSend.setText("");
				
	        	TextView textLog = (TextView)findViewById(R.id.textLog);
	        	textLog.append("Send :");
	        	textLog.append(sBlabla);
	        	textLog.append("\n");
	        	
				_SPPClient.sendMessage(sBlabla);	        	
			}
        });
        Button buttonSendGetVersion = (Button)findViewById(R.id.buttonSendGetVersion);
        buttonSendGetVersion.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
	        	TextView textLog = (TextView)findViewById(R.id.textLog);
	        	textLog.append("Send :");
	        	textLog.append(BluetoothCommandProtocol.sendAskVersion());
	        	textLog.append("\n");
	        	
				_SPPClient.sendMessage(BluetoothCommandProtocol.sendAskVersion());	        	
			}
        });
        Button buttonStartReport = (Button)findViewById(R.id.buttonStartReport);
        buttonStartReport.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
	        	TextView textLog = (TextView)findViewById(R.id.textLog);
	        	textLog.append("Send :");
	        	textLog.append(BluetoothCommandProtocol.sendSetSendStatus(true));
	        	textLog.append("\n");

	        	_SPPClient.sendMessage(BluetoothCommandProtocol.sendSetSendStatus(true));	        	
			}
        });
        Button buttonStopReport = (Button)findViewById(R.id.buttonStopReport);
        buttonStopReport.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
	        	TextView textLog = (TextView)findViewById(R.id.textLog);
	        	textLog.append("Send :");
	        	textLog.append(BluetoothCommandProtocol.sendSetSendStatus(false));
	        	textLog.append("\n");

	        	_SPPClient.sendMessage(BluetoothCommandProtocol.sendSetSendStatus(false));	        	
			}
        });
        Button buttonTurrel = (Button)findViewById(R.id.buttonTurrel);
        buttonTurrel.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
	        	TextView textLog = (TextView)findViewById(R.id.textLog);
	        	textLog.append("Send :");
	        	textLog.append(BluetoothCommandProtocol.sendAskStatusTurret());
	        	textLog.append("\n");

	        	_SPPClient.sendMessage(BluetoothCommandProtocol.sendAskStatusTurret());	        	
			}
        });

        LinearLayout layoutInTest = (LinearLayout)findViewById(R.id.layoutInTest);
        layoutInTest.setVisibility(View.GONE);
        LinearLayout layoutSelect = (LinearLayout)findViewById(R.id.layoutSelect);
    	layoutSelect.setVisibility(View.VISIBLE);
        LinearLayout layoutTest = (LinearLayout)findViewById(R.id.layoutTest);
		layoutTest.setVisibility(View.GONE);
        LinearLayout layoutCancel = (LinearLayout)findViewById(R.id.layoutCancel);
		layoutCancel.setVisibility(View.GONE);

        GetAdapter();
    }

	protected void LaunchTest() {
        LinearLayout layoutInTest = (LinearLayout)findViewById(R.id.layoutInTest);
        layoutInTest.setVisibility(View.VISIBLE);
		LinearLayout layoutSelect = (LinearLayout)findViewById(R.id.layoutSelect);
    	layoutSelect.setVisibility(View.GONE);
		LinearLayout layoutTest = (LinearLayout)findViewById(R.id.layoutTest);
		layoutTest.setVisibility(View.GONE);
		LinearLayout layoutCancel = (LinearLayout)findViewById(R.id.layoutCancel);
		layoutCancel.setVisibility(View.VISIBLE);
		
		Connect();
	}

    protected void CancelTest() {
        LinearLayout layoutInTest = (LinearLayout)findViewById(R.id.layoutInTest);
        layoutInTest.setVisibility(View.GONE);
        LinearLayout layoutSelect = (LinearLayout)findViewById(R.id.layoutSelect);
    	layoutSelect.setVisibility(View.VISIBLE);
        LinearLayout layoutTest = (LinearLayout)findViewById(R.id.layoutTest);
		layoutTest.setVisibility(View.GONE);
        LinearLayout layoutCancel = (LinearLayout)findViewById(R.id.layoutCancel);
		layoutCancel.setVisibility(View.GONE);
	}

	@Override
    public void onStart() {
        super.onStart();

		if (!mBluetoothAdapter.isEnabled()) {
		    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
		    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		    return;
		}
	}
	
	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data)
	{
		switch(requestCode)
		{
		case REQUEST_ENABLE_BT:
			if(resultCode == RESULT_OK)
			{
				Connect();
			}
			break;
		case GETTING_DEVICE:
			if(resultCode == RESULT_OK)
			{
				String sAddressDevice = data.getStringExtra(DeviceList_Activity.EXTRA_DEVICE_ADDRESS);
				TextView textAddressDevice = (TextView)findViewById(R.id.textAddressDevice);
				textAddressDevice.setText(sAddressDevice);
		        
				LinearLayout layoutSelect = (LinearLayout)findViewById(R.id.layoutSelect);
		    	layoutSelect.setVisibility(View.GONE);
				LinearLayout layoutTest = (LinearLayout)findViewById(R.id.layoutTest);
				layoutTest.setVisibility(View.VISIBLE);
				LinearLayout layoutCancel = (LinearLayout)findViewById(R.id.layoutCancel);
				layoutCancel.setVisibility(View.GONE);
			}
			break;
		}
	}

	private void GetAdapter() {
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (mBluetoothAdapter == null) {
		    Toast.makeText(this, "No bluetooth adapter :(", Toast.LENGTH_LONG).show(); 
			return;
		}
	}

	final SPPEvent_Client mBTHandler = new SPPEvent_Client()
	{

		@Override
		public void onConnected() {
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_CONNECTED, null));
		}

		@Override
		public void onDisconnected() {
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_DISCONNECTED, null));
		}

		@Override
		public void onError(String sErr) {
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_GETERROR, sErr));
		}

		@Override
		public void onText(String sText) {
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_GETTEXT, sText));
		}

		@Override
		public void onCmd(String sCmd) {
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_GETCMD, sCmd));
		}		
	};
	
	final Handler _UI_Handler = new Handler() {
	    @Override
		public void handleMessage(Message msg) {
	        switch (msg.what) {
	        case MESSAGE_UI_CONNECTED:
	        {
	        	TextView textLog = (TextView)findViewById(R.id.textLog);
	        	textLog.append("Connected\n");
	        }
	            break;

	        case MESSAGE_UI_DISCONNECTED:
	        {
	        	TextView textLog = (TextView)findViewById(R.id.textLog);
	        	textLog.append("Disconnected !\n");
	        }
	            break;

	        case MESSAGE_UI_GETERROR:
	        {
	        	TextView textLog = (TextView)findViewById(R.id.textLog);
	        	textLog.append("Get error : " + (String) msg.obj + "\n");
	        }
	        	break;
            
	        case MESSAGE_UI_GETTEXT:
	        {
	        	TextView textLog = (TextView)findViewById(R.id.textLog);
	        	textLog.append("Get text :");
	        	textLog.append((String) msg.obj);
	        	textLog.append("\n");
	        }
	            break;
	        case MESSAGE_UI_GETCMD:
	        {
	        	TextView textLog = (TextView)findViewById(R.id.textLog);
	        	textLog.append("Get cmd :");
	        	textLog.append((String) msg.obj);
	        	textLog.append("\n");
	        }
	            break;

	        }
	    }
	};

    private void Connect() {
    	String sAddressDevice = ((TextView)findViewById(R.id.textAddressDevice)).getText().toString();
    	if(BluetoothAdapter.checkBluetoothAddress(sAddressDevice))
    	{
    		if(!_SPPClient.connect(sAddressDevice))
    		{
    			Toast.makeText(this, "Cannot connect :(", Toast.LENGTH_LONG).show();    			
    		}
    	}
	}
}